package org.javaforever.gatescore.reports.verb;

import java.util.ArrayList;
import java.util.List;

import org.javaforever.gatescore.core.FrontCodeBlock;
import org.javaforever.gatescore.core.FrontDomain;
import org.javaforever.gatescore.core.FrontDropdown;
import org.javaforever.gatescore.core.FrontField;
import org.javaforever.gatescore.core.FrontMethod;
import org.javaforever.gatescore.core.FrontVerb;
import org.javaforever.gatescore.core.Statement;
import org.javaforever.gatescore.core.Writeable;
import org.javaforever.gatescore.core.WriteableUtil;
import org.javaforever.gatescore.utils.FieldUtil;
import org.javaforever.gatescore.utils.StringUtil;

public class ListActiveSumOption extends FrontVerb{
	protected List<FrontField> xAxisFields = new ArrayList<>();
	protected FrontField yName;

	public ListActiveSumOption(FrontDomain domain, List<FrontField> xAxisFields, FrontField yName) {
		super();
		this.domain = domain;
		this.xAxisFields = xAxisFields;
		this.yName = yName;
		this.setVerbName("ListActive"+this.domain.getCapFirstPlural()+"SumOption");
		this.setLabel("列出活跃记录图表");
	}

	public List<FrontField> getxAxisFields() {
		return xAxisFields;
	}

	public void setxAxisFields(List<FrontField> xAxisFields) {
		this.xAxisFields = xAxisFields;
	}

	public FrontField getyName() {
		return yName;
	}

	public void setyName(FrontField yName) {
		this.yName = yName;
	}
	@Override
	public FrontMethod generateControllerMethod() throws Exception {
		FrontMethod method = new FrontMethod();
		method.setStandardName("listActive"+StringUtil.capFirst(this.domain.getCapFirstPlural()+"SumOption"));
		//method.addSignature(new Signature(1,"chartstype","var"));
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,0,"listActive"+this.domain.getCapFirstPlural()+"SumOption(chartstype) {"));
		sList.add(new Statement(2000L,1,"var option = {}"));
		sList.add(new Statement(3000L,1,"option.title = { text: '' }"));
		sList.add(new Statement(4000L,1,"option.tooltip = {}"));
		sList.add(new Statement(5000L,1,"option.xAxis = { data: ["+FieldUtil.genFieldArrayTextSingleQuotedStr(this.xAxisFields)+"] }"));
		sList.add(new Statement(6000L,1,"option.yAxis = {}"));
		sList.add(new Statement(7000L,1,"var planData = []"));
		sList.add(new Statement(8000L,1,"var legendData = []"));
		sList.add(new Statement(10000L,1,"for (let ii = 0; ii < this.active"+this.domain.getCapFirstPlural()+".length; ii++) {"));
		sList.add(new Statement(11000L,2,"let item = this.active"+this.domain.getCapFirstPlural()+"[ii]"));
		sList.add(new Statement(12000L,2,"var itemdata = {}"));
		
		if (this.yName instanceof FrontDropdown) {
			sList.add(new Statement(13000L,2,"itemdata.name = this.translate"+((FrontDropdown)this.yName).getTarget().getCapFirstDomainName()+"(item."+this.yName.getLowerFirstFieldName()+")"));
		}else {
			sList.add(new Statement(13000L,2,"itemdata.name = item."+this.yName.getLowerFirstFieldName()));
		}
		sList.add(new Statement(15000L,2,"legendData.push(itemdata.name)"));
		sList.add(new Statement(16000L,2,"itemdata.type = chartstype"));		
		sList.add(new Statement(17000L,2,"var itemdatadata = ["+FieldUtil.genFieldArrayLowerFirstNameWithPrefixStr(this.xAxisFields, "item.")+"]"));
		sList.add(new Statement(18000L,2,"var itemdatasumdata = []"));
		sList.add(new Statement(19000L,2,"for (var i = 0; i < itemdatadata.length; i++) {"));
		sList.add(new Statement(20000L,3,"var sum = 0"));
		sList.add(new Statement(21000L,3,"for (var j = 0; j <= i; j++) {"));
		sList.add(new Statement(22000L,4,"sum += itemdatadata[j]"));
		sList.add(new Statement(23000L,3,"}"));
		sList.add(new Statement(24000L,3,"itemdatasumdata.push(sum)"));
		sList.add(new Statement(25000L,2,"}"));
		sList.add(new Statement(26000L,2,"itemdata.data = itemdatasumdata"));
		sList.add(new Statement(27000L,2,"planData.push(itemdata)"));
		sList.add(new Statement(28000L,1,"}"));
		sList.add(new Statement(30000L,1,"option.legend = {}"));
		sList.add(new Statement(31000L,1,"option.legend.data = legendData"));
		sList.add(new Statement(32000L,1,"option.series = planData"));
		sList.add(new Statement(33000L,1,"this.option = option"));
		sList.add(new Statement(34000L,0,"},"));

		method.setMethodStatementList(WriteableUtil.merge(sList));
		return method;
	}
	@Override
	public FrontMethod generateApiMethod() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public FrontCodeBlock generateRouteBlockBlock() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public FrontMethod generateControllerMethod(String indexSuffix) throws Exception {
		return generateControllerMethod();
	}
}
